package cn.stronglink.assetmanage.business.check.assetlist;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import org.w3c.dom.ls.LSInput;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.listener.onRecyclerListener;
import cn.stronglink.assetmanage.utils.ImageUtils;

/**
 * 已入库资产列表
 * Author by chencongcong,Date on 2018/9/20
 */
public class CheckAssetAdapter extends RecyclerView.Adapter<CheckAssetAdapter.CheckAssetHolder> {
    private List<Asset> mAssetList;
    private Context mContext;
    private onRecyclerListener mListener;

    public void setListener(onRecyclerListener listener) {
        mListener = listener;
    }

    public CheckAssetAdapter(Context context) {
        mContext = context;
    }

    @Override
    public CheckAssetHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemView = inflater.inflate(R.layout.item_check_asset_list, null, true);
        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                60);
        itemView.setLayoutParams(lp);
        return new CheckAssetHolder(itemView, mListener);
    }

    @Override
    public void onBindViewHolder(CheckAssetHolder holder, int position) {
        if (mAssetList != null) {
            if (position % 2 == 0) {
                //偶数
                holder.ll_check_item.setBackgroundColor(mContext.getResources().getColor(R.color.colorWhite));
            } else {
                holder.ll_check_item.setBackgroundColor(mContext.getResources().getColor(R.color.gray_check_item));
            }
            holder.tvCheckSerialNo.setText(String.valueOf(position + 1));
            holder.tvCheckAssetNo.setText(mAssetList.get(position).getAssetNo());
            holder.tvCheckAssetName.setText(mAssetList.get(position).getAssetName());
            holder.tvCheckAssetRfid.setText(mAssetList.get(position).getRfidNo());
            holder.tvCheckAssetLocation.setText(mAssetList.get(position).getRoomName());
            holder.tvCheckAssetSn.setText(mAssetList.get(position).getSerialNo());
            holder.ivCheckAssetPicture.setVisibility(View.INVISIBLE);
            String url = mAssetList.get(position).getPictureUri();
            if (url != null) {
                holder.ivCheckAssetPicture.setVisibility(View.VISIBLE);
                RequestOptions options = new RequestOptions()
                        .centerCrop()
                        .placeholder(R.color.color_f6)
                        .diskCacheStrategy(DiskCacheStrategy.ALL);
                Glide.with(mContext)
                        .load(url)
                        .apply(options)
                        .into(holder.ivCheckAssetPicture);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mAssetList == null ? 0 : mAssetList.size();
    }

    public class CheckAssetHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private onRecyclerListener mListener;
        @BindView(R.id.tv_check_serial_no)
        TextView tvCheckSerialNo;
        @BindView(R.id.tv_check_asset_no)
        TextView tvCheckAssetNo;
        @BindView(R.id.tv_check_asset_name)
        TextView tvCheckAssetName;
        @BindView(R.id.tv_check_asset_rfid)
        TextView tvCheckAssetRfid;
        @BindView(R.id.tv_check_asset_location)
        TextView tvCheckAssetLocation;
        @BindView(R.id.tv_check_asset_sn)
        TextView tvCheckAssetSn;
        @BindView(R.id.iv_check_asset_picture)
        ImageView ivCheckAssetPicture;
        @BindView(R.id.ll_check_item)
        LinearLayout ll_check_item;

        public CheckAssetHolder(View itemView, onRecyclerListener listener) {
            super(itemView);
            this.mListener = listener;
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClicked(v, getAdapterPosition());
            }
        }
    }


    public void notifyDataChanged(List<Asset> mAssetList) {
        this.mAssetList = mAssetList;
        this.notifyDataSetChanged();
    }
}
